#include "screen.h"
#include <windows.h>
#include <iostream>
#include <conio.h>
#define MaxMenuLength 15
 #pragma warning(disable : 4996)


using namespace std;

void screen::gotoxy(int x,int y)
{
HANDLE console_handle;
COORD cursor_coord;
DWORD actual=0;

cursor_coord.X=x;
cursor_coord.Y=y;

console_handle= GetStdHandle( STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(console_handle,cursor_coord);
}

void screen::settextcolor(int color)
{
 HANDLE  hColor;	
 hColor = GetStdHandle (STD_OUTPUT_HANDLE);
 SetConsoleTextAttribute(hColor,color); 
}

void screen::puttext(char *s,int x,int y,int c)
{
gotoxy(x,y);
settextcolor(c);
cout<<s;
settextcolor(0x07);
}

void screen::puttext(int i,int x,int y,int c)
{
gotoxy(x,y);
settextcolor(c);
cout<<i;
settextcolor(0x07);
}

void screen::box(int x1,int y1,int x2,int y2)
{
for (int i=x1;i<x2;i++)
{
 gotoxy(i,y1);
 cout<<(char)LineX;
 gotoxy(i,y2);
 cout<<(char)LineX;
}

for (int i=y1;i<y2;i++)
{
 gotoxy(x1,i);
 cout<<(char)LineY;
 gotoxy(x2,i);
 cout<<(char)LineY;
}

gotoxy(x1,y1);
cout<<(char)Line1;
gotoxy(x2,y1);
cout<<(char)Line2;
gotoxy(x2,y2);
cout<<(char)Line3;
gotoxy(x1,y2);
cout<<(char)Line4;

}
int screen::menu(char **Arr,int size,int starti,char *Header,int StartY)
{
char ch;
int ind=starti,passindex=0,offset=0;

placemenu(Arr,size,StartY,starti,Header,0);
do
{

ch = getch();
 
if (ch==27 || ch==13)
	break;

if (ch==0,ch==72)
	{
		ind--;
		if (ind<0)
			ind=size-1;
		offset = (ind>MaxMenuLength-1)?(ind-(MaxMenuLength-1)):(0);
		placemenu(Arr,size,StartY,ind,Header,offset);
	}
if (ch==0,ch==80)	
	{
		ind++;
		if (ind>=size)
			ind = 0;

		offset = (ind>MaxMenuLength-1)?(ind-(MaxMenuLength-1)):(0);
		placemenu(Arr,size,StartY,ind,Header,offset);
	}

}
while(ch!=27 && ch!=13);

if (ch==27)
	return -1;
else
	return ind;

}

void screen::placemenu(char **Arr,int size,int ystart,int index,char *header,int offset)
{
	int maxstringlength;
	int x0,x1,y0,y1,i,j,xt;
	char tmpstr[50];
	int menusize;
	menusize = (size>MaxMenuLength)?(MaxMenuLength):(size);
	maxstringlength = getmaxmenulength(Arr,size)+2;
	x0 = 40-(maxstringlength/2) ;
	x1 = 40+(maxstringlength/2)+1 ;
	y0 = ystart -1;
    y1 = ystart + menusize;
	settextcolor(0x0007);
	box(x0,y0,x1,y1);
	for(i=0;i<menusize;i++)
	{
	strcpy(tmpstr,"");
	xt = 40 - (int)strlen(Arr[i+offset])/2 + 1;
	for(j=x0;j<xt-1;j++)
		strcat(tmpstr," ");
	strcat(tmpstr,Arr[i+offset]);
	for(j=xt+(int)strlen(Arr[i+offset]);j<x1;j++)
		strcat(tmpstr," ");

	gotoxy(x0+1,ystart+i);
	
	if ((index!=i+offset))
		settextcolor(0x0007);
	else
		settextcolor(0x0070);

	cout<<tmpstr;
	}

	if (strlen(header)>0)
	{
		y0=ystart-1;
		x0 = 40 - ((int)strlen(header)+4)/2 + 1;
		settextcolor(0x0e);
		gotoxy(x0,y0);
		cout<<"[ "<<header<<" ]";
	}

settextcolor(0x0007);
	
}

int screen::getmaxmenulength(char **A,int size)
{
int i;
int max=0;
for(i=0;i<size;i++)
  if ((int)strlen(A[i])>max)
	  max = (int)strlen(A[i]);

return max;
}
char *screen::readvalue(char *title,char *range,int masked,int maxsize,char *msg)
{
char *tmpstr=NULL,ch,i;
int count=0,x1,y1=13;

system("cls");
box(0,11,79,15);

if (msg!=NULL)
{
x1 = 40 - (int)strlen(msg) / 2 + 1;
puttext(msg,x1,y1+2,0x0c);
}

puttext(title,20,y1,0x0f);
x1 = (int)strlen(title) + 20;

//showcursor(TRUE);
tmpstr = new char[50];//(char*)malloc(sizeof(char)*50);
strcpy(tmpstr,"");
settextcolor(0x000c);// string red
do
{
ch=getch();
if (count<maxsize)
  if (strchr(range,ch)!=NULL)
	{	
		count++;
		//tmpstr = (char*)realloc(tmpstr,sizeof(char)*(count+1));
        tmpstr[count-1]=ch;
		tmpstr[count]=0;
		gotoxy(x1,y1);
		if (!masked) cout<<tmpstr;//printf("%s",tmpstr);
		else
			for(i=0;i<(int)strlen(tmpstr);i++)
				cout<<"*";//printf("*");
	}
 if (ch==8 && count>0)
   {
		count--;
		tmpstr[count]=' ';
		gotoxy(x1,y1);
		if (!masked)
			cout<<tmpstr;//printf("%s",tmpstr);
		else
		{
			for(i=0;i<(int)strlen(tmpstr)-1;i++)
				cout<<"*";//printf("*");
				cout<<" ";//printf(" ");
		}

		//tmpstr = (char*)realloc(tmpstr,sizeof(char)*(count+1));
		tmpstr[count]=0;
		gotoxy(x1,y1);
		if (!masked) cout<<tmpstr;//printf("%s",tmpstr);
		else
			for(i=0;i<(int)strlen(tmpstr);i++)
				cout<<"*";//printf("*");
   }

 if (ch==27)
	 //if(!AskYesNoQuestion("Are you want to exit ?"))
	 {
		 ch=0;
  		 gotoxy(x1+(int)strlen(tmpstr),y1);
		 settextcolor(0x000c);// string red
		 //showcursor(TRUE);
	 }
	 if (ch==13)
		 if ((int)strlen(tmpstr)>0)
			 break;

}

while (ch!=27);

//showcursor(FALSE);
settextcolor(0x07);
if (count==0 || ch==27)
{
	if (ch==27 && (int)strlen(tmpstr)>0)
		free(tmpstr);
	return NULL;
}
else
	return tmpstr;

}

void screen::cls()
{
	system("cls");
}
void screen::Stars()
{
int x,y;

for(int i=0;i<100;i++)
{
	x = (rand() % (76 - 3 + 1)) + 3; 
	y = (rand() % (23 - 2 + 1)) + 2; 
	settextcolor((x+y)%15);
	gotoxy(x,y);
	printf(".");
}
gotoxy(0,0);
settextcolor(0x07);
}